{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Git\n",
    "\n",
    "[図解Git](https://marklodato.github.io/visual-git-guide/index-ja.html)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gitディレクトリの初期化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initialized empty Git repository in /root/python/jupyter_notebook/Git/gittest/.git/\n",
      "On branch master\n",
      "\n",
      "Initial commit\n",
      "\n",
      "nothing to commit (create/copy files and use \"git add\" to track)\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### いったん全部消す\n",
    "rm -rf ${directory}\n",
    "\n",
    "### git用ディレクトリの作成とgit initの実行\n",
    "mkdir ${directory}\n",
    "cd ${directory}\n",
    "git init\n",
    "git status"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## (ここでgitコマンドを実行する)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "On branch master\n",
      "\n",
      "Initial commit\n",
      "\n",
      "Changes to be committed:\n",
      "  (use \"git rm --cached <file>...\" to unstage)\n",
      "\n",
      "\tnew file:   hoge.txt\n",
      "\n",
      "[master (root-commit) 0e47948] update\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 hoge.txt\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "cd ${directory}\n",
    "echo \"fuga\" > hoge.txt\n",
    "git add hoge.txt\n",
    "git status\n",
    "git commit -m \"update\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## gitディレクトリの構造確認"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".git\n",
      ".git/refs\n",
      ".git/refs/heads\n",
      ".git/refs/heads/master\n",
      ".git/refs/tags\n",
      ".git/branches\n",
      ".git/description\n",
      ".git/hooks\n",
      ".git/hooks/applypatch-msg.sample\n",
      ".git/hooks/commit-msg.sample\n",
      ".git/hooks/post-update.sample\n",
      ".git/hooks/pre-applypatch.sample\n",
      ".git/hooks/pre-commit.sample\n",
      ".git/hooks/pre-push.sample\n",
      ".git/hooks/pre-rebase.sample\n",
      ".git/hooks/prepare-commit-msg.sample\n",
      ".git/hooks/update.sample\n",
      ".git/info\n",
      ".git/info/exclude\n",
      ".git/HEAD\n",
      ".git/config\n",
      ".git/objects\n",
      ".git/objects/pack\n",
      ".git/objects/info\n",
      ".git/objects/91\n",
      ".git/objects/91/28c3eb56a3547e2cca631042366bf0f37abe67\n",
      ".git/objects/1b\n",
      ".git/objects/1b/df3476cbd778b37d809fbba71cf7cbd0fc4b0e\n",
      ".git/objects/0e\n",
      ".git/objects/0e/47948dfcd1234ab4fc241d3edcf7e8ba51c104\n",
      ".git/index\n",
      ".git/COMMIT_EDITMSG\n",
      ".git/logs\n",
      ".git/logs/refs\n",
      ".git/logs/refs/heads\n",
      ".git/logs/refs/heads/master\n",
      ".git/logs/HEAD\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### ディレクトリ構造の確認\n",
    "cd ${directory}\n",
    "find .git"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## .git直下のファイル\n",
    "\n",
    "* `git add` を実行したとき\n",
    "    * HEAD, config, descriptionは何も変わらない\n",
    "    * indexファイルが作成される\n",
    "        * バイナリファイル\n",
    "        * プロジェクトのある時点でのディレクトリツリー全体を表すデータをもつ\n",
    "        * プロジェクトの各ファイルについて対応する以下のものを保存する\n",
    "            * ブロブへのポインタ\n",
    "            * プロジェクトルートディレクトリからの相対パス\n",
    "        * ls-filesコマンド\n",
    "            * --stage\n",
    "                * mode bits, オブジェクト名, stage number, ファイル名を表示\n",
    "        * stage(ステージ)する\n",
    "            * 別の表現\n",
    "                * 特定の変更内容をindexに登録する\n",
    "                * <font color=\"red\">次回コミットに含めるようgitに指示する</font>\n",
    "            * コマンド\n",
    "                * git add\n",
    "            * stageを利用する事のメリット\n",
    "                * ひとつのコミットには主題となる変更とは無関係な変更を含めないというポリシーを実現しやすくなる\n",
    "        * index\n",
    "            * ワーキングディレクトリからstageした内容を保持する領域\n",
    "                * ワーキングディレクトリ\n",
    "                    * 別名\n",
    "                        * ワークツリー\n",
    "                        * ワークディレクトリ\n",
    "                        * ワーキングコピー\n",
    "                    * 実際に作業しているディレクトリ\n",
    "            * リポジトリと、ワーキングディレクトリとの間にある緩衝地帯\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### .git/HEAD\n",
      "================================================\n",
      "ref: refs/heads/master\n",
      "================================================\n",
      "\n",
      "### .git/config\n",
      "================================================\n",
      "[core]\n",
      "\trepositoryformatversion = 0\n",
      "\tfilemode = true\n",
      "\tbare = false\n",
      "\tlogallrefupdates = true\n",
      "================================================\n",
      "\n",
      "### .git/description\n",
      "================================================\n",
      "Unnamed repository; edit this file 'description' to name the repository.\n",
      "================================================\n",
      "\n",
      "### .git/index\n",
      "================================================\n",
      "DIRC\u0000\u0000\u0000\u0002\u0000\u0000\u0000\u0001YC��\u00040��YC��\u00040��\u0000\u0000�\u0004\u0001�m\u0005\u0000\u0000��\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0005�(��V�T~,�c\u0010B6k��z�g\u0000\bhoge.txt\u0000\u0000b�����t�9�������s.p\u001a\n",
      "================================================\n",
      "\n",
      "### git ls-files --stage\n",
      "================================================\n",
      "100644 9128c3eb56a3547e2cca631042366bf0f37abe67 0\thoge.txt\n",
      "================================================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### ファイルの確認\n",
    "cd ${directory}\n",
    "echo \"### .git/HEAD\"\n",
    "echo \"================================================\"\n",
    "cat .git/HEAD\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### .git/config\"\n",
    "echo \"================================================\"\n",
    "cat .git/config\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### .git/description\"\n",
    "echo \"================================================\"\n",
    "cat .git/description\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "if [ -f \".git/index\" ]; then\n",
    "echo \"### .git/index\"\n",
    "echo \"================================================\"\n",
    "cat .git/index\n",
    "echo \n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### git ls-files --stage\"\n",
    "echo \"================================================\"\n",
    "git ls-files --stage\n",
    "echo \"================================================\"\n",
    "echo\n",
    "fi\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## branchesディレクトリ\n",
    "\n",
    "* `git add` を実行したとき\n",
    "    * 何も変わらない"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### .git/branches\n",
      "================================================\n",
      "================================================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### branchesディレクトリの確認\n",
    "cd ${directory}\n",
    "echo \"### .git/branches\"\n",
    "echo \"================================================\"\n",
    "ls .git/branches\n",
    "echo \"================================================\"\n",
    "echo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## hooksディレクトリ\n",
    "\n",
    "* `git add` を実行したとき\n",
    "    * 何も変わらない"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### .git/hooks\n",
      "================================================\n",
      "applypatch-msg.sample\n",
      "commit-msg.sample\n",
      "post-update.sample\n",
      "pre-applypatch.sample\n",
      "pre-commit.sample\n",
      "pre-push.sample\n",
      "pre-rebase.sample\n",
      "prepare-commit-msg.sample\n",
      "update.sample\n",
      "================================================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### hooksディレクトリの確認\n",
    "cd ${directory}\n",
    "echo \"### .git/hooks\"\n",
    "echo \"================================================\"\n",
    "ls .git/hooks\n",
    "echo \"================================================\"\n",
    "echo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## infoディレクトリ\n",
    "\n",
    "* `git add` を実行したとき\n",
    "    * 何も変わらない"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### .git/info\n",
      "================================================\n",
      "exclude\n",
      "================================================\n",
      "\n",
      "### .git/info/exclude\n",
      "================================================\n",
      "# git ls-files --others --exclude-from=.git/info/exclude\n",
      "# Lines that start with '#' are comments.\n",
      "# For a project mostly in C, the following would be a good set of\n",
      "# exclude patterns (uncomment them if you want to use them):\n",
      "# *.[oa]\n",
      "# *~\n",
      "================================================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### infoディレクトリの確認\n",
    "cd ${directory}\n",
    "echo \"### .git/info\"\n",
    "echo \"================================================\"\n",
    "ls .git/info\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### .git/info/exclude\"\n",
    "echo \"================================================\"\n",
    "cat .git/info/exclude\n",
    "echo \"================================================\"\n",
    "echo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## objectsディレクトリ\n",
    "\n",
    "* `git add` を実行したとき\n",
    "    * 91ディレクトリが作成される\n",
    "    * コミットのハッシュ値のファイルができる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### .git/objects\n",
      "================================================\n",
      "0e\n",
      "1b\n",
      "91\n",
      "info\n",
      "pack\n",
      "================================================\n",
      "\n",
      "### .git/objects/info\n",
      "================================================\n",
      "================================================\n",
      "\n",
      "### .git/objects/pack\n",
      "================================================\n",
      "================================================\n",
      "\n",
      "### .git/objects/91\n",
      "================================================\n",
      "28c3eb56a3547e2cca631042366bf0f37abe67\n",
      "================================================\n",
      "\n",
      "### cat .git/objects/91/28c3eb56a3547e2cca631042366bf0f37abe67\n",
      "================================================\n",
      "x\u0001K��OR0eH+MO�\u0002\u0000\u0019V\u0003�\n",
      "================================================\n",
      "\n",
      "### git cat-file -p 9128c3eb56a3547e2cca631042366bf0f37abe67\n",
      "================================================\n",
      "fuga\n",
      "================================================\n",
      "\n",
      "### git cat-file -t 9128c3eb56a3547e2cca631042366bf0f37abe67\n",
      "================================================\n",
      "blob\n",
      "================================================\n",
      "\n",
      "### git cat-file -s 9128c3eb56a3547e2cca631042366bf0f37abe67\n",
      "================================================\n",
      "5\n",
      "================================================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### objectsディレクトリの確認\n",
    "cd ${directory}\n",
    "echo \"### .git/objects\"\n",
    "echo \"================================================\"\n",
    "ls .git/objects\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### .git/objects/info\"\n",
    "echo \"================================================\"\n",
    "ls .git/objects/info\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### .git/objects/pack\"\n",
    "echo \"================================================\"\n",
    "ls .git/objects/pack\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "if [ -d \".git/objects/91\" ]; then\n",
    "echo \"### .git/objects/91\"\n",
    "echo \"================================================\"\n",
    "ls .git/objects/91\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "commit_hash_value=`ls .git/objects/91`\n",
    "\n",
    "echo \"### cat .git/objects/91/${commit_hash_value}\"\n",
    "echo \"================================================\"\n",
    "cat .git/objects/91/${commit_hash_value}\n",
    "echo\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### git cat-file -p 91${commit_hash_value}\"\n",
    "echo \"================================================\"\n",
    "git cat-file -p 91${commit_hash_value}\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### git cat-file -t 91${commit_hash_value}\"\n",
    "echo \"================================================\"\n",
    "git cat-file -t 91${commit_hash_value}\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### git cat-file -s 91${commit_hash_value}\"\n",
    "echo \"================================================\"\n",
    "git cat-file -s 91${commit_hash_value}\n",
    "echo \"================================================\"\n",
    "echo\n",
    "fi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## refsディレクトリ\n",
    "\n",
    "* `git add` を実行したとき\n",
    "    * 何も変化なし\n",
    "* `git commit`を実行したとき\n",
    "    * headsの下にmasterができる\n",
    "    * HEAD\n",
    "        * 普通の状態ではHEADはmasterへのリファレンス\n",
    "            * masterは「ブランチ」とか呼ばれているけど、物理的には特定のコミットオブジェクトへのリファレンス"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "### .git/refs\n",
      "================================================\n",
      "heads\n",
      "tags\n",
      "================================================\n",
      "\n",
      "### .git/refs/heads\n",
      "================================================\n",
      "master\n",
      "================================================\n",
      "\n",
      "### .git/refs/tags\n",
      "================================================\n",
      "================================================\n",
      "\n",
      "### cat.git/refs/heads/master\n",
      "================================================\n",
      "0e47948dfcd1234ab4fc241d3edcf7e8ba51c104\n",
      "================================================\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### refsディレクトリの確認\n",
    "cd ${directory}\n",
    "echo \"### .git/refs\"\n",
    "echo \"================================================\"\n",
    "ls .git/refs\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### .git/refs/heads\"\n",
    "echo \"================================================\"\n",
    "ls .git/refs/heads\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "echo \"### .git/refs/tags\"\n",
    "echo \"================================================\"\n",
    "ls .git/refs/tags\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "if [ -f \".git/refs/heads/master\" ]; then\n",
    "echo \"### cat.git/refs/heads/master\"\n",
    "echo \"================================================\"\n",
    "cat .git/refs/heads/master\n",
    "echo \"================================================\"\n",
    "echo\n",
    "fi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## branchAをbranchA_oldにリネームする\n",
    "\n",
    "* git branch -m <古いブランチ名> <新しいブランチ名>\n",
    "* ただし、リネームしたブランチをpushしてもリポジトリ上の昔のブランチは削除されない"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initialized empty Git repository in /root/python/jupyter_notebook/Git/gittest/.git/\n",
      "On branch master\n",
      "\n",
      "Initial commit\n",
      "\n",
      "nothing to commit (create/copy files and use \"git add\" to track)\n",
      "================================================\n",
      "[master (root-commit) 7ab63bb] update master\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 master.txt\n",
      "On branch master\n",
      "nothing to commit, working directory clean\n",
      "total 4\n",
      "-rw-r--r-- 1 root root 7 Jun 19 10:56 master.txt\n",
      "================================================\n",
      "\n",
      "================================================\n",
      "[branchA 1480b7d] update branchA\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 branchA.txt\n",
      "On branch branchA\n",
      "nothing to commit, working directory clean\n",
      "total 8\n",
      "-rw-r--r-- 1 root root 8 Jun 19 10:56 branchA.txt\n",
      "-rw-r--r-- 1 root root 7 Jun 19 10:56 master.txt\n",
      "================================================\n",
      "\n",
      "================================================\n",
      "* branchA_old\n",
      "  master\n",
      "total 8\n",
      "-rw-r--r-- 1 root root 8 Jun 19 10:56 branchA.txt\n",
      "-rw-r--r-- 1 root root 7 Jun 19 10:56 master.txt\n",
      "================================================\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Switched to branch 'branchA'\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### いったん全部消す\n",
    "rm -rf ${directory}\n",
    "\n",
    "### git用ディレクトリの作成とgit initの実行\n",
    "mkdir ${directory}\n",
    "cd ${directory}\n",
    "git init\n",
    "git status\n",
    "\n",
    "### masterにmaster.txtファイルを作成\n",
    "echo \"================================================\"\n",
    "echo \"master\" > master.txt\n",
    "git add master.txt\n",
    "git commit -m \"update master\"\n",
    "git status\n",
    "ls -l\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "### masterからbranchAを作成し、branchA.txtの作成\n",
    "echo \"================================================\"\n",
    "git branch branchA\n",
    "git checkout branchA\n",
    "echo \"branchA\" > branchA.txt\n",
    "git add branchA.txt\n",
    "git commit -m \"update branchA\"\n",
    "git status\n",
    "ls -l\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "### branchAをbranchA_oldにリネーム\n",
    "echo \"================================================\"\n",
    "git branch -m branchA branchA_old\n",
    "git branch -a\n",
    "ls -l\n",
    "echo \"================================================\"\n",
    "echo\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## リポジトリ上のブランチを削除する\n",
    "\n",
    "* git push origin :branchA\n",
    "\n",
    "## ブランチから1ファイルだけ持ってくる\n",
    "\n",
    "* 確認\n",
    "    * git show <ブランチ名>:<ファイル名>\n",
    "* 取ってくる\n",
    "    * git checkout <ブランチ名> -- <ファイル名>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## branchAで作業しているつもりがbranchBで作業した特定のcommitだけbranchAに持ってくる方法\n",
    "\n",
    "* branchB上のcommitを確認し、branchAに移動してcherry-pickでマージする"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Initialized empty Git repository in /root/python/jupyter_notebook/Git/gittest/.git/\n",
      "On branch master\n",
      "\n",
      "Initial commit\n",
      "\n",
      "nothing to commit (create/copy files and use \"git add\" to track)\n",
      "================================================\n",
      "[master (root-commit) adab8ff] update master\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 master.txt\n",
      "On branch master\n",
      "nothing to commit, working directory clean\n",
      "total 4\n",
      "-rw-r--r-- 1 root root 7 Jun 19 10:52 master.txt\n",
      "================================================\n",
      "\n",
      "================================================\n",
      "[branchA 8dd25b6] update branchA\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 branchA.txt\n",
      "On branch branchA\n",
      "nothing to commit, working directory clean\n",
      "total 8\n",
      "-rw-r--r-- 1 root root 8 Jun 19 10:52 branchA.txt\n",
      "-rw-r--r-- 1 root root 7 Jun 19 10:52 master.txt\n",
      "================================================\n",
      "\n",
      "================================================\n",
      "[branchB 18b722b] update branchB\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 branchB.txt\n",
      "On branch branchB\n",
      "nothing to commit, working directory clean\n",
      "total 8\n",
      "-rw-r--r-- 1 root root 8 Jun 19 10:52 branchB.txt\n",
      "-rw-r--r-- 1 root root 7 Jun 19 10:52 master.txt\n",
      "================================================\n",
      "\n",
      "================================================\n",
      "[branchB c4f3959] update branchA add_branchA.txt\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 add_branchA.txt\n",
      "On branch branchB\n",
      "nothing to commit, working directory clean\n",
      "total 12\n",
      "-rw-r--r-- 1 root root 12 Jun 19 10:52 add_branchA.txt\n",
      "-rw-r--r-- 1 root root  8 Jun 19 10:52 branchB.txt\n",
      "-rw-r--r-- 1 root root  7 Jun 19 10:52 master.txt\n",
      "\n",
      "commit c4f3959332cc24a891e138f50dccbaa5253136df\n",
      "================================================\n",
      "\n",
      "================================================\n",
      "[branchA 1a184aa] update branchA add_branchA.txt\n",
      " Date: Mon Jun 19 10:52:34 2017 +0900\n",
      " 1 file changed, 1 insertion(+)\n",
      " create mode 100644 add_branchA.txt\n",
      "total 12\n",
      "-rw-r--r-- 1 root root 12 Jun 19 10:52 add_branchA.txt\n",
      "-rw-r--r-- 1 root root  8 Jun 19 10:52 branchA.txt\n",
      "-rw-r--r-- 1 root root  7 Jun 19 10:52 master.txt\n",
      "================================================\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Switched to branch 'branchA'\n",
      "Switched to branch 'master'\n",
      "Switched to branch 'branchB'\n",
      "Switched to branch 'branchA'\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "directory=gittest\n",
    "\n",
    "### いったん全部消す\n",
    "rm -rf ${directory}\n",
    "\n",
    "### git用ディレクトリの作成とgit initの実行\n",
    "mkdir ${directory}\n",
    "cd ${directory}\n",
    "git init\n",
    "git status\n",
    "\n",
    "### masterにmaster.txtファイルを作成\n",
    "echo \"================================================\"\n",
    "echo \"master\" > master.txt\n",
    "git add master.txt\n",
    "git commit -m \"update master\"\n",
    "git status\n",
    "ls -l\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "### masterからbranchAを作成し、branchA.txtの作成\n",
    "echo \"================================================\"\n",
    "git branch branchA\n",
    "git checkout branchA\n",
    "echo \"branchA\" > branchA.txt\n",
    "git add branchA.txt\n",
    "git commit -m \"update branchA\"\n",
    "git status\n",
    "ls -l\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "### masterからbranchBを作成し、branchB.txtの作成\n",
    "echo \"================================================\"\n",
    "git checkout master\n",
    "git branch branchB\n",
    "git checkout branchB\n",
    "echo \"branchB\" > branchB.txt\n",
    "git add branchB.txt\n",
    "git commit -m \"update branchB\"\n",
    "git status\n",
    "ls -l\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "### branchB上で add_branchA.txtファイルを誤ってcommit\n",
    "echo \"================================================\"\n",
    "echo \"add branchA\" > add_branchA.txt\n",
    "git add add_branchA.txt\n",
    "git commit -m \"update branchA add_branchA.txt\"\n",
    "git status\n",
    "ls -l\n",
    "echo \n",
    "git log | grep commit | head -1\n",
    "target_commit=`git log | grep commit | head -1 | awk '{print $2}'`\n",
    "echo \"================================================\"\n",
    "echo\n",
    "\n",
    "### branchAに移動してcherry-pickでマージする\n",
    "echo \"================================================\"\n",
    "git checkout branchA\n",
    "git cherry-pick ${target_commit}\n",
    "ls -l\n",
    "echo \"================================================\"\n",
    "echo\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
